package com.gitee.kinbug.sniper.web.device;

import java.math.BigDecimal;
import java.math.RoundingMode;
import java.util.ArrayList;
import java.util.List;
import java.util.NavigableMap;
import java.util.stream.Collectors;

import com.gitee.kinbug.sniper.common.entity.Bar;
import com.gitee.kinbug.sniper.common.entity.Boll;
import com.gitee.kinbug.sniper.common.enums.CandleEnum;
import com.gitee.kinbug.sniper.common.enums.SymbolEnum;
import com.gitee.kinbug.sniper.indicator.BollIndicator;
import com.gitee.kinbug.sniper.indicator.RSIIndicator;

/**
 * -提供指标的计算和访问源头
 * @ClassName: Index
 * @author kinbug
 * @date 2021-05-17
 */
public class Index extends AbstractIndex {
	
	public Index(SymbolEnum symbolEnum) {
		super(symbolEnum);
	}
	/**
	 * -获取当前RSI指标
	 * @author kinbug
	 * @param candleEnum K线类型
	 * @return Double
	 */
	public BigDecimal getRsi(CandleEnum candleEnum) {
		if (candleEnum == CandleEnum.MIN5) {
			return this.getSingleRsi(barMapM5, 1);
		} else if (candleEnum == CandleEnum.MIN15) {
			return this.getSingleRsi(barMapM15, 1);
		} else if (candleEnum == CandleEnum.MIN30) {
			return this.getSingleRsi(barMapM30, 1);
		} else if (candleEnum == CandleEnum.HOUR1) {
			return this.getSingleRsi(barMapH1, 1);
		} else if (candleEnum == CandleEnum.HOUR4) {
			return this.getSingleRsi(barMapH4, 1);
		}
		return null;
	}

	/**
	 * -获取当前Boll指标
	 * @param candleEnum K线类型
	 * @return Boll boll相关指标值
	 */
	public Boll getBoll(CandleEnum candleEnum) {
		if (candleEnum == CandleEnum.MIN5) {
			return this.getSingleBoll(barMapM5);
		} else if (candleEnum == CandleEnum.MIN15) {
			return this.getSingleBoll(barMapM15);
		} else if (candleEnum == CandleEnum.MIN30) {
			return this.getSingleBoll(barMapM30);
		} else if (candleEnum == CandleEnum.HOUR1) {
			return this.getSingleBoll(barMapH1);
		} else if (candleEnum == CandleEnum.HOUR4) {
			return this.getSingleBoll(barMapH4);
		}
		return null;
	}

	// barMap：K线数据源；num：获取第几个指标
	private BigDecimal getSingleRsi(NavigableMap<Long, Bar> barMap, int num) {
		List<BigDecimal> risList = new ArrayList<BigDecimal>();
		List<Bar> barList = new ArrayList<Bar>(barMap.values());
		barList = barList.subList(Math.max(barList.size() - 100, 0), barList.size());
		// 获取14个周期的rsi
		RSIIndicator.getRsiValue(barList, 14, risList);
		return risList.get(risList.size() - num);
	}

	// barMap：K线数据源；num：获取第几个指标
	private Boll getSingleBoll(NavigableMap<Long, Bar> barMap) {
		Boll boll = new Boll();
		List<Bar> barList = new ArrayList<Bar>(barMap.values());
		// 获取20个周期的bar的boll信息
		List<Bar> listCore = barList.subList(barList.size() - 20, barList.size());
		List<BigDecimal> list = listCore.stream().map(a -> a.getClose()).collect(Collectors.toList());
		BigDecimal mbValue = BollIndicator.getMbValue(list).setScale(symbolEnum.getPrecision(), RoundingMode.DOWN);
		BigDecimal upboll = BollIndicator.getUpValue(list, mbValue, DECIMAL2).setScale(symbolEnum.getPrecision(), RoundingMode.DOWN);
		BigDecimal dnboll = BollIndicator.getDnValue(list, mbValue, DECIMAL2).setScale(symbolEnum.getPrecision(), RoundingMode.DOWN);
		BigDecimal bbw = (upboll.subtract(dnboll)).divide(mbValue, symbolEnum.getPrecision(), RoundingMode.DOWN);
		boll.setMb(mbValue).setUp(upboll).setDn(dnboll).setBbw(bbw);
		return boll;
	}
}
